/**
 * Created by kaki on 2017/9/28.
 */
import wepy from 'wepy'
import { api, storageKey, host } from 'src/config'
import http from 'src/utils/http'

// 发送短信验证码到手机
export const requestSms = (mobile) => {
  const url = host + 'sms/code'
  const data = {
    mobile
  }
  return http.post(url, data)
}

export const requestBindMobile = (mobile, code) => {
  const url = host + 'user/bind/mobile'
  const data = {
    mobile,
    code
  }
  return http.post(url, data)
}

export const bindMiniProgramMobile = (encryptedData, iv) => {
  const url = host + 'user/bind/mobile/mini_program'
  const data = {
    encryptedData,
    iv
  }
  return http.post(url, data)
}

export const upLoad = (file, strategy = 'avatar') => {
  const url = host + `files/upload?strategy=${strategy}`
  const data = {
    file
  }
  return http.post(url, data)
}

export async function requestMe (refresh = false, include = 'cards') {
  try {
    let meInfo = {}
    let cacheMeInfo = wepy.getStorageSync(storageKey.me)
    if (!refresh && cacheMeInfo) {
      return cacheMeInfo
    }
    const data = {
      include
    }
    meInfo = await http.get(api.user.me.url, data)
    await wepy.setStorage({
      key: storageKey.me,
      data: meInfo.data
    })
    return meInfo
  } catch (e) {
    wepy.showModal({
      title: '提示',
      content: `获取用户信息失败，请关闭重新进入。${e.message}`,
      showCancel: false
    })
  }
}
